package com.liukai.todo_android.entity;

import com.liukai.todo_android.common.constants.Result;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一返回实体
 *
 * @author Freeman Liu
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@ToString
@Accessors(chain = true)
public class Resp implements Serializable {
    private int code;
    private String message;
    private Object data;

    public Resp(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Resp(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Resp(int code) {
        this.code = code;
    }

    public static Resp ok() {
        return new Resp(200);
    }

    public static Resp ok(String message) {
        return new Resp(200, message);
    }

    public static Resp ok(int code, String message) {
        return new Resp(200, message, null);
    }

    public static Resp ok(String message, Object data) {
        return new Resp(200, message, data);
    }

    public static Resp ok(int code, String message, Object data) {
        return new Resp(code, message, data);
    }

    public static Resp err() {
        return new Resp(500);
    }

    public static Resp err(String message) {
        return new Resp(500, message);
    }

    public static Resp err(int code, String message) {
        return new Resp(500, message, null);
    }

    public static Resp err(String message, Object data) {
        return new Resp(500, message, data);
    }

    public static Resp err(int code, String message, Object data) {
        return new Resp(code, message, data);
    }

    public Resp message(String message) {
        this.message = message;
        return this;
    }

    public Resp data(Object data) {
        this.data = data;
        return this;
    }

    public Resp code(int code) {
        this.code = code;
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static Resp ok(Result result) {
        return Resp.ok(result.getCode(), result.getMessage());
    }

    public static Resp err(Result result) {
        return Resp.err(result.getCode(), result.getMessage());
    }

    @Override
    public String toString() {
        return "Resp{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
